import {BasicColumn, FormSchema} from '@apps/antd-admin/src/components/Table';
import {Api as ModelApi} from '../../outstore/data/model.api';
import {Api as BrandApi} from "../../brand/data/brand.api";
import {Api as BscApi} from "../../bcs/data/bcs.api";
import {Api} from "./car.api";
import {render} from "@apps/antd-admin/src/hooks/render/renderUtils";

export const columns: BasicColumn[] = [
  {
    title: '车辆图片',
    dataIndex: 'carImages',
    width: 200,
    customRender: render.renderImage,
  },
  {
    title: '所属品牌',
    dataIndex: 'brandName',
    width: 100,
  },
  {
    title: '所属车系',
    dataIndex: 'seriesName',
    width: 100,
  },
  {
    title: '标题',
    dataIndex: 'carTitle',
    width: 200,
    customRender:render.renderTip
  },
  {
    title: '上牌时间',
    dataIndex: 'licensingDate',
    width: 100,
  },
  {
    title: '里程(万公里)',
    dataIndex: 'mileage',
    width: 120,
  },
  {
    title: '上牌城市',
    dataIndex: 'areaName',
    width: 100,
  },
  {
    title: '预估金额(万元)',
    dataIndex: 'predictAmount',
    width: 150,
  },
  {
    title: '分期期数(期)',
    dataIndex: 'periodization',
    width: 100,
  },
  {
    title: '首付(万元)',
    dataIndex: 'oldAmount',
    width: 100,
  },
  {
    title: '过户次数',
    dataIndex: 'transfer',
    width: 100,
  },
  {
    title: '发布时间',
    dataIndex: 'sendDate',
    width: 200,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 200,
  },
  {
    title: '上架',
    dataIndex: 'status',
    width: 80,
    sorter:true,
    renderApi: Api.setState,
    rowKey: 'carId',
    fixed: 'right',
    customRender: render.renderSwitch
  },
  {
    title: '推荐',
    dataIndex: 'recommend',
    width: 80,
    renderApi: Api.setRecommend,
    rowKey: 'carId',
    fixed: 'right',
    customRender: render.renderSwitch,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'brandId',
    label: '品牌',
    component: 'ApiSelect',
    colProps: {span: 3},
    componentProps: ({formModel, formActionType}) => {
      return {
        api: BrandApi.list,
        labelField: 'brandName',
        valueField: 'brandId',
        immediate: true,
        showSearch: true,
        onChange(brandId) {
          formModel.seriesId = null;
          const {updateSchema} = formActionType;
          updateSchema(
              {
                field: 'seriesId',
                componentProps: {
                  api: BscApi.list,
                  immediate: false,
                  labelField: 'seriesName',
                  valueField: 'seriesId',
                  showSearch: true,
                  params: {
                    brandId: brandId
                  }
                },
              }
          );
        },
      };
    },
  }, {
    field: 'seriesId',
    label: '车系',
    component: 'ApiSelect',
    colProps: {span: 3}
  },
  {
    field: 'carTitle',
    label: '标题',
    component: 'Input',
    colProps: {span: 3},
  },
  {
    field: 'status',
    label: '上架状态',
    component: 'Select',
    colProps: {span: 3},
    componentProps:{
      options: [
        { label: '未上架', value: 0 },
        { label: '已上架', value: 1 },
      ],
    }
  },
  {
    field: 'recommend',
    label: '推荐状态',
    component: 'Select',
    colProps: {span: 3},
    componentProps:{
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    }
  }
];

export const formSchema: FormSchema[] = [
  {
    label: '所属车型',
    field: 'modelId',
    component: 'ApiModalSelect',
    required: true,
    componentProps: ({formModel, formActionType}) => {
      return {
        onSuccess: (options, values, selectRows) => {
          formModel.carTitle = selectRows[0].modelName
        },
        selectConfig: {
          multiple: '',
          api: ModelApi.page,
          rowKey: 'carModelId',
          labelKey: 'modelName',
          modalTitle: '选择车型',
          searchForm: [
            {
              field: 'brandId',
              label: '品牌',
              component: 'ApiSelect',
              colProps: {span: 2},
              componentProps: ({formModel, formActionType}) => {
                return {
                  api: BrandApi.list,
                  labelField: 'brandName',
                  valueField: 'brandId',
                  immediate: true,
                  showSearch: true,
                  onChange(brandId) {
                    formModel.seriesId = null;
                    const {updateSchema} = formActionType;
                    updateSchema(
                        {
                          field: 'seriesId',
                          componentProps: {
                            api: BscApi.list,
                            immediate: false,
                            labelField: 'seriesName',
                            valueField: 'seriesId',
                            showSearch: true,
                            params: {
                              brandId: brandId
                            }
                          },
                        }
                    );
                  },
                };
              },
            }, {
              field: 'seriesId',
              label: '车系',
              component: 'ApiSelect',
              colProps: {span: 2}
            },
            {
              label: '车型名称',
              field: 'modelName',
              component: 'Input',
            },
          ],
          columns: [
            {
              title: '车型名称',
              dataIndex: 'modelName',
              width: 150,
            },
            {
              title: '品牌',
              dataIndex: 'brandName',
              width: 100,
            },
            {
              title: '车系',
              dataIndex: 'seriesName',
              width: 100,
            }
          ],
        }
      }
    }
  },

  {
    label: '标题',
    field: 'carTitle',
    component: 'Input',
    required: true,
  },
  {
    label: '上牌时间',
    field: 'licensingDate',
    component: 'Input',
    required: true,
  },

  {
    label: '里程数',
    field: 'mileage',
    component: 'InputNumber',
    required: true,
    componentProps: {
      precision: 2,
      min: 0,
      addonAfter: '万公里'
    }
  },
  {
    label: '上牌城市',
    field: 'cityId',
    component: 'ApiSelect',
    required: true,
    componentProps: ({formModel, formActionType}) => {
      return {
        api: '/area/getAllCity',
        labelField: 'label',
        getPopupContainer: () => document.body,
        valueField: 'value',
        immediate: true,
        showSearch: true
      };
    },
  },
  {
    label: '预估金额',
    field: 'predictAmount',
    component: 'InputNumber',
    required: true,
    componentProps: {
      precision: 2,
      min: 0,
      addonAfter: '万元'
    }
  },
  {
    label: '分期期数',
    field: 'periodization',
    component: 'InputNumber',
    componentProps: {
      min: 1,
      addonAfter: '期'
    }
  },
  {
    label: '首付',
    field: 'oldAmount',
    component: 'InputNumber',
    required: true,
    componentProps: {
      precision: 2,
      min: 0,
      addonAfter: '万元'
    }
  },
  {
    label: '过户次数',
    field: 'transfer',
    component: 'InputNumber',
    wrapperCol: {
      span: 8
    },
    componentProps: {
      min: 0,
      addonAfter: '次'
    },
    required: true,
  },
  {
    label: '车辆图片',
    field: 'carImages',
    component: 'ImageUpload',
    required: true,
    componentProps: {
      fileMax: 10,
    },
    itemProps:{
      help:'上传图片大小尺寸 750*566，最多上传10张',
    }
  },
  {
    label: '描述',
    field: 'reaImages',
    component: 'TyEditor',
    required: false,
  },
];
